/*
 * @Author: jackning 270580156@qq.com
 * @Date: 2024-01-19 10:29:49
 * @LastEditors: jackning 270580156@qq.com
 * @LastEditTime: 2025-01-04 14:31:42
 * @Description: bytedesk.com https://github.com/Bytedesk/bytedesk
 *   Please be aware of the BSL license restrictions before installing Bytedesk IM – 
 *  selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license. 
 *  仅支持企业内部员工自用，严禁私自用于销售、二次销售或者部署SaaS方式销售 
 *  Business Source License 1.1: https://github.com/Bytedesk/bytedesk/blob/main/LICENSE 
 *  contact: 270580156@qq.com 
 * 联系：270580156@qq.com
 * Copyright (c) 2024 by bytedesk.com, All Rights Reserved. 
 */
import Login from "@/pages/Auth/Login";
import Register from "@/pages/Auth/Register";
import Home from "@/pages/Dashboard/Home";
import Dashboard from "@/pages/Dashboard";
import Setting from "@/pages/Dashboard/Setting";
import {
  createBrowserRouter,
  createHashRouter,
} from "react-router-dom";
import Auth from "@/pages/Auth";
// import Robot from "@/pages/Dashboard/Robot";
import Profile from "@/pages/Dashboard/Setting/Profile";
import Task from "@/pages/Dashboard/Plugins/Task";
import Collect from "@/pages/Dashboard/Plugins/Collect";
import Basic from "@/pages/Dashboard/Setting/Basic";
import Plugins from "@/pages/Dashboard/Plugins";
import ServerSwitch from "@/pages/Auth/Server";
import NoFoundPage from "@/pages/404";
// import { basename } from "path";
import { IS_ELECTRON } from "@/utils/constants";
import Second from "@/pages/Dashboard/Second";

function RequireAuth({ children }: { children: React.ReactNode }) {
  // const { isLoggedIn } = useContext(AppContext);
  // const location = useLocation();
  // console.log("isLoggedIn", isLoggedIn);
  // return isLoggedIn ? (
  //   children
  // ) : (
  //   <Navigate to="/auth/login" replace state={{ from: location }} />
  // );
  return children;
}

const routes = [
  {
    path: "/",
    element: (
      <>
        <RequireAuth>
          <Dashboard />
        </RequireAuth>
      </>
    ),
    children: [
      {
        path: "/",
        element: <Home />,
      },
      {
        path: "/home",
        element: <Home />,
      },
      {
        path: "/second",
        element: <Second />,
      },
      {
        path: "/plugins",
        element: <Plugins />,
        children: [
          {
            path: "/plugins",
            element: <Collect />,
          },
          {
            path: "/plugins/collect",
            element: <Collect />,
          },
          {
            path: "/plugins/task",
            element: <Task />,
          },
        ],
      },
      {
        path: "/setting",
        element: <Setting />,
        children: [
          {
            path: "/setting",
            element: <Basic />,
          },
          {
            path: "/setting/profile",
            element: <Profile />,
          },
          {
            path: "/setting/basic",
            element: <Basic />,
          },
        ],
      },
    ],
  },
  {
    path: "/auth",
    element: <Auth />,
    children: [
      {
        path: "/auth",
        element: <Login isModel={false} />,
      },
      {
        path: "/auth/login",
        element: <Login isModel={false} />,
      },
      {
        path: "/auth/register",
        element: <Register />,
      },
      {
        path: "/auth/server",
        element: <ServerSwitch />,
      },
    ],
  },
  // {
  //   path: "/enlarge",
  //   element: <Enlarge />,
  // },
  {
    path: "*",
    element: <NoFoundPage />,
  },
];
//
let router;
if (IS_ELECTRON) {
  router = createHashRouter(routes, {
    future: {
      v7_normalizeFormMethod: true,
      v7_relativeSplatPath: true,
      v7_partialHydration: true,
      v7_fetcherPersist: true,
      v7_skipActionErrorRevalidation: true,
      // v7_startTransition: true,
    },
  });
} else {
  // 方便/agent/robot页面左侧分类列表中使用锚点定位，所以才切换到createBrowserRouter
 router = createBrowserRouter(routes,
    {
      basename: "/agent/",
      future: {
        v7_normalizeFormMethod: true,
        v7_relativeSplatPath: true,
        v7_partialHydration: true,
        v7_fetcherPersist: true,
        v7_skipActionErrorRevalidation: true,
        // v7_startTransition: true,
      },
    },
  );
}
// 
export default router;
